Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • graph combine | graphregion spacing in two-panel graph

    Hello all:

    I was writing a wrapper for standard two panel figure of K-M plots oriented vertically for publications. I like the fxsize and fysize numbers I set to get the proportions I like for the KM plot.

    While the individual graphs look fine, the combined version looks strange with extra white space in the graph region on both left and right sides. I need to trim the extra space in the graph region of the combined figure. How can I fix the code to achieve this? I have looked at all the options of graph combine and twoway and can't seem to get it right.


    Code:
            graph combine A B, col(1) xcommon iscale(.7) ///
             imargin(medium) fxsize(80) fysize(150) graphregion(margin(l-25 r-25)) name(AB, replace)
    Click image for larger version

Name:	Figure_1.png
Views:	2
Size:	176.6 KB
ID:	1739808
    Attached Files

  • #2
    After much fiddling around, I figured it out. Sadly it was mostly trial and error. I had to work with xsize and ysize options too to get it right. If anyone wants, feel free to use the code after your have your survival graphs labels A, B, C or D in memory. I will still have to make small help file so I don't forget what I coded.
    Glad to to take any critical suggestions for improving it. Most medical journal mandate this alphabetic paneling labels and I had to figure out a way to do it within Stata.

    Code:
    *! v1.0-- Initial version14jan2024->GV
    
    *------------------------------------------------------------------------------
    * SUBPROGRAM FOR PANELING 2, 3 or 4 PANELS
    *------------------------------------------------------------------------------
    *Syntax with four arguments only
    *--------------------------------
    * gspanel arg1 arg2 arg3 arg4
    
    /*
    Options              Description                   Choices (each separated by space)
    *-------------------------------------------------------------------------------
    arg1 (tots)            No. of figures in panel      2 3 4
    arg2 (name)            Figure name in quotes        "Figure_1"-- may add it as a directory path
    arg3 (risktable)     risktable in graph?            no yes risktable
    arg4 (orientation)    for 2 and 3-panels            horizontal, vertical 
    *-------------------------------------------------------------------------------
    */
    
    /*--syntax sample
    gspanel 2 "Figure_1" yes vertical
    */
    
    cap program drop gspanel
    program define gspanel
        args tots name risktable orient         // 
    
        if inlist("`risktable'"," ","no") {
            if `tots' == 2 {
                if "`orient'" == "horizontal" {
                    graph combine A B, row(1) xcommon ycommon iscale(0.9) imargin(large) ///
                    plotregion(margin(l-6 r-5 t-4 b-5)) ///
                     graphregion(margin(l-1 r-1 t-1 b-3))  xsize(8in) ysize(4in)name(AB, replace)
                    graph export "${prfold}//outputs/`name'.png", width(2400) height(1200) replace as(png)
                    graph export "./`name'.pdf", replace as(pdf)
                }
                else if "`orient'" == "vertical" {
                    graph combine A B, col(1)  iscale(1) ///
                     imargin(large) fxsize(65) fysize(130) plotregion(margin(l-24 r-23 t-31 b-29)) ///
                     graphregion(margin(l-5 r-5 t-15 b-15))  xsize(4in) ysize(8in) name(AB, replace)
                    graph export "${prfold}//outputs/`name'.png", width(1600) height(2400) replace as(png)
                    graph export ".//`name'.pdf", replace as(pdf)
                }
            }
            if `tots'== 4 {
                graph combine A B C D, imargin(large) iscale(0.55) ///
                plotregion(margin(l-3 r-4 t-6 b-6)) ///
                 graphregion(margin(l-1 r-1 t+1 b+1))  xsize(8in) ysize(6in) name(ABCD, replace)
                graph export "${prfold}//outputs/`name'.png", width(2400) height(1600) replace as(png)
                graph export ".//`name'.pdf", replace as(pdf)
            }
        else if `tots'== 3 {
                graph combine A B C, col(1)  iscale(0.9) ///
                 imargin(large) fxsize(65) fysize(150) plotregion(margin(l-16 r-20 t-30 b-29)) ///
                 graphregion(margin(l-1 r-1 t+1 b+1))  xsize(8in) ysize(16in) name(AB, replace)
                graph export "${prfold}//outputs/`name'.png", width(1600) height(3200) replace as(png)
                graph export ".//`name'.pdf", replace as(pdf)
            }
        }
    
    
    
        else if inlist("`risktable'","risktable","yes") {
            if `tots' == 2 {
                if "`orient'" == "horizontal" {
                graph combine A B, row(1) xcommon ycommon iscale(0.8) imargin(large) ///
                plotregion(margin(l-3 r-3 t-2 b-4)) ///
                 graphregion(margin(l=4 r=4 t-1 b-3))  xsize(8in) ysize(4in)name(AB, replace)
                graph export "${prfold}//outputs/`name'.png", width(2400) height(1200) replace as(png)
                graph export ".//`name'.pdf", replace as(pdf)
                }
                else if "`orient'" == "vertical" {
                graph combine A B, col(1)  iscale(0.9) ///
                 imargin(large) fxsize(65) fysize(150) plotregion(margin(l-20 r-20 t-28 b-29)) ///
                 graphregion(margin(l-5 r-5 t-14 b-15))  xsize(4in) ysize(8in) name(AB, replace)
                graph export "${prfold}//outputs/`name'.png", width(1600) height(2400) replace as(png)
                graph export ".//`name'.pdf", replace as(pdf)
                }
    
            }
            if `tots'== 4 {
                graph combine A B C D, imargin(large) iscale(.5) ///
                plotregion(margin(l-3 r-4 t-6 b-6)) ///
                 graphregion(margin(l=5 r=5 t+1 b+1))  xsize(8in) ysize(6in) name(ABCD, replace)
                graph export "${prfold}//outputs/`name'.png", width(2400) height(1600) replace as(png)
                graph export ".//`name'.pdf", replace as(pdf)
            }
            else if `tots'== 3 {
                graph combine A B C, col(1)  iscale(.8) ///
                 imargin(large) fxsize(65) fysize(150) plotregion(margin(l-16 r-20 t-30 b-29)) ///
                 graphregion(margin(l-1 r-1 t+1 b+1))  xsize(8in) ysize(16in) name(AB, replace)
                graph export "${prfold}/outputs/`name'.png", width(1600) height(3200) replace as(png)
                graph export ".//`name'.pdf", replace as(pdf)
            }
        }
    cls
            * Display header text
    *-----------------------------------------------------------------------------
    
        di _dup(75) "-"
        di as text _col(15) "Program " as res "-gspanel-" as text " for " as res "`tots'" as text " panels"
        di _dup(75) "-"
        di as text "Output file name(s)        : " _column(15) as res "`name'" as text "-PNG & PDF"
        di as text "Output PNG file directory  : " _column(15) as res "{Project Folder}//outputs"
        di as text "Output PDF file directory  : " _column(15) as res "{Project Folder}//manuscripts"
        di as text "Plot options used          : " _column(15) as text "risktable" as res "(`risktable'); " as text "Orient-ation" as res "(`orient')"
        if `tots'== 3 {
        di as text "Warning(s)                 : " _column(15) di as error " `tots' panels and horizontal is not allowed"
        di as text "Orientation changed to     : " _column(15) as res " Vertical"
        }
        di _dup(75) "-"
    
    *-----------------------------------------------------------------------------
    end

    Comment

    Working...
    X